<?php
// +----------------------------------------------------------------------
// | Author: Stupboy King
// +----------------------------------------------------------------------
namespace app\api\model;
use app\api\model\Common;
class OrderFee extends Common
{
    /**
     * 订单费用计算
     * @param $oid 订单id
     */
    public function countFee($oid){
        $fee1 = ' wait_fee + start_fee + time_fee + travel_fee + remote_fee';
        $fee1.= ' + low_fee + night_fee + high_fee + toll + park_fee + other_fee + cancel_fee';
        $fee2 = ' wait_fee2 + start_fee2 + time_fee2 + travel_fee2 + remote_fee2';
        $fee2.= ' + low_fee2 + night_fee2 + high_fee + toll + park_fee + other_fee + cancel_fee';
        $data['driver_money']   = ['exp',$fee2];
        $data['passenger_money']= ['exp',$fee1];
        $data['company_money']  = ['exp',$fee1.' - ('.$fee2.')'];
        $this->save($data,['order_id'=>$oid]);
    }

    /**
     * 获取司机费用
     * @param $oid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getDriverFee($oid){
        $this->countFee($oid);
        $field = 'start_fee2 start_fee,travel_fee2 travel_fee,meter,use_time,time_fee2 time_fee,low_speed_time low_time';
        $field.= ',low_fee2 low_fee,remote_meter,remote_fee2 remote_fee,wait_time,wait_fee2 wait_fee,driver_money';
        $field.= ',toll,park_fee,other_fee,high_fee';
        return $this->where('order_id',$oid)->field($field)->find();
    }

    /**
     * 获取乘客费用
     * @param $oid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getPassengerFee($oid){
        $this->countFee($oid);
        $field = 'start_fee,travel_fee,meter,use_time, time_fee,low_speed_time low_time';
        $field.= ', low_fee,remote_meter, remote_fee,wait_time, wait_fee,driver_money';
        $field.= ',toll,park_fee,other_fee,high_fee';
        return $this->where('order_id',$oid)->field($field)->find();
    }

    /**
     * 更新订单费用
     * @param $oid
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateFee($oid,$data){
        $fee = $this->where('order_id',$oid)->field('order_id')->find();
        if(empty($fee)){
            $data['order_id'] = $oid;
            $this->save($data);
        }else{
            $this->save($data,['order_id'=>$oid]);
        }
    }
}